from logging import exception
from time import sleep, strftime,localtime,time
from selenium.webdriver.common.by import By
from common.Utils import ForumDriver
from selenium.common.exceptions import NoSuchFrameException

scrpath = "C:\\ForumTest\\Screenshot"

#论坛用户登录界面对象
class ForumLoginTest:
    driver = ""
    url = ""
    def __init__(self):
        self.driver = ForumDriver.driver
        self.url = "http://127.0.0.1:58080/sign-in.html"
        self.driver.get(self.url)

    def loginUI(self):
        try:
            act1 = self.driver.find_element(By.CSS_SELECTOR,"#username")
        except NoSuchFrameException:
            return False
        try:
            act2 = self.driver.find_element(By.CSS_SELECTOR,"#password")
        except NoSuchFrameException:
            return False
        try:
            act3 = self.driver.find_element(By.CSS_SELECTOR,"#submit")
        except NoSuchFrameException:
            return False
        file_name = strftime("%Y-%m-%d %H-%M-%S loginUI", localtime(time())) + ".png"
        path = scrpath + "\\" + file_name
        self.driver.get_screenshot_as_file(path)




    def LoginFunc(self,username,password):
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(username)
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys(password)
        sleep(2)
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        sleep(1)
        file_name = strftime("%Y-%m-%d %H-%M-%S LoginFunc", localtime(time())) + ".png"
        path = scrpath + "\\" + file_name
        self.driver.get_screenshot_as_file(path)
        sleep(2)
        if self.driver.current_url == "http://127.0.0.1:58080/index.html":
            return None
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()


# L = ForumLoginTest()
# L.loginUI()
# sleep(2)
# L.LoginFunc("zhangsan","123")
# sleep(2)
# L.LoginFunc("zhangsa","123")
# sleep(2)
# L.LoginFunc("zhangsan","12")
# sleep(2)
# L.LoginFunc("zhangsa","12")
# sleep(2)
# L.driver.quit()